#pragma once
#include <map>
#include <gl/glew.h>
#include <sfml/Graphics/RenderWindow.hpp>

#include "Singleton.h"

class BaseState;

/*
	Application.
	Tis class creates the game windows and calls Update and Draw methods 
	of the currentState (E.I.: MainMenu,Game,PauseMenu). It also init the ortho view of the
	world and counts fps.
	In order to change currenState use SetState Method.
*/

class Application : public Singleton<Application>
{
public:
	void Init(bool fullscreen = false);
	void Destroy(void);

	// SETTERS
	void							SetState(const std::string& state);
	inline void						Exit(){ m_window->Close();}

	void							Run();
	void							Render();

	//GETTERS
	inline const sf::RenderWindow* 	Window()const{ return m_window; }
	BaseState*						GetState(const std::string& stateName);
	BaseState*						GetCurrentState()const{ return m_currentState; }
	std::string						GetCurrentStateName()const{ return m_currentStateName; }

	//Input
	bool						IsInputAvailable(){ return m_clock.GetElapsedTime() >= m_inputPauseTime; }
	void						PauseInput(float time=0.15f){  m_inputPauseTime = m_clock.GetElapsedTime() + time;}

private:
	
	void						Init3D();
	void						Prepare3DViewport(int left, int top, int width, int height);

	
	void						OnResize(unsigned int width, unsigned int height);


	typedef std::map<std::string,BaseState*>	StateMap;
	StateMap m_states;

	sf::Clock					m_clock;
	float						m_inputPauseTime;
	int							m_frames;
	float						m_lastFrameCheck;
	std::string					m_fps;

	sf::RenderWindow*			m_window;

	BaseState*					m_currentState;
	std::string					m_currentStateName;

};
